/**
 * Created by mrcode on 2015/12/13.
 */

$(function () {
    initMd();
    initMdToc();
    sendRecord()
});

function initMd() {
    var userAccount = $("#userAccount").val();
    var articleId = $("#articleId").val();

    var testEditormdView;
    var bodyT = "/assets/lib/md/test.md";
    var bodySorc = "/" + userAccount + "/article/details/body/" + articleId;
    $.ajax({
        url: bodySorc,
//				data:{Full:"fu"},
        type: "POST",
        dataType: 'text',
        success: function (markdown) {
            testEditormdView = editormd.markdownToHTML("test-editormd-view", {
                markdown: markdown,//+ "\r\n" + $("#append-test").text(),
                path: '/assets/lib/md/lib/',
                //htmlDecode      : true,       // 开启 HTML 标签解析，为了安全性，默认不开启
                htmlDecode: "style,script,iframe",  // you can filter tags decode
                toc: true,
                tocm: true,    // Using [TOCM]
                mrcodeBootstrapTocContainer: "#md-toc",
//                    tocContainer: "#custom-toc-container", // 自定义 ToC 容器层
//						gfm             : false,
//						tocDropdown     : true,
                // markdownSourceCode : true, // 是否保留 Markdown 源码，即是否删除保存源码的 Textarea 标签
                emoji: false,
                taskList: true,
                tex: true,  // 默认不解析
                flowChart: true,  // 默认不解析
                sequenceDiagram: true,  // 默认不解析
            });
            //console.log("返回一个 jQuery 实例 =>", testEditormdView);

            // 获取Markdown源码
            //console.log(testEditormdView.getMarkdown());

            //alert(testEditormdView.getMarkdown());
            //$(".markdown-toc-list").clone().appendTo($("#custom-toc-container"));
        },
        error: function (er) {
            console.log("发生了一个错误" + er);
        }
    });
}

/**
 * 初始化右侧文章目录
 */
function initMdToc() {
    $(document).ready(function () {
        $('#stickUpToc').stickUp("");
        var clientWidth = document.body.clientWidth * 0.65;
        $("#md-toc").css({"max-height":clientWidth +"px"})
    });
    $("#toc_btn").on("click",function () {
        var tb = $(this).find("span");
        if(tb.hasClass("icon-caret-up")){
            tb.removeClass("icon-caret-up");
            tb.addClass("icon-caret-down");
            cloneCatlog();
        }else{
            tb.removeClass("icon-caret-down");
            tb.addClass("icon-caret-up");
            openCatlog()
        }
    });

    /** 打开目录 */
    function openCatlog() {
        $("#md-toc").show(500);
    }

    /** 关闭目录 */
    function cloneCatlog() {
        $("#md-toc").hide(500);
    }

    //监视滚动
    $('body').scrollspy({
        target: '#md-toc',
        offset: 80  //滚动高度偏移量
    });
    $('#md-toc').on('activate.bs.scrollspy', function () {
        console.log(JSON.stringify($(this)));
    })

    $('[data-spy="scroll"]').each(function () {
        var $spy = $(this).scrollspy('refresh')
    })
}

function sendRecord() {
    $.post($contextPath + "/stat/article", {
        type: 1,
        id: $("#articleId").val()
    });
}

function delArticle_open(id, userAccount) {
    MrcodeUi.Alert.popLayer.popYNCenter("要把该文章删除并放入回收站吗？","取消","确定",function (me) {
        me.close();
        delArticle(id, userAccount);
    })
}
function delArticle(id, userAccount) {
    var pop = null;
     $(document).ajaxStart(function() {
        pop = MrcodeUi.Alert.popLayer.popLoading();
    });
    $(document).ajaxStop(function() {
        pop.close();
    });
    $.ajax({
        url: '/auth/article/delete/' + id + "/1/3",
        type: 'DELETE',
        dataType: 'json',
        success: function (result) {
            MrcodeUi.Alert.popLayer.popLoading(result.msg + "<br/>自动跳转中,请稍后... ");
            window.location.href = "/" + userAccount + "/article/list";
        },
        error: function (XMLHttpRequest, errinfo, ex) {
            MrcodeUi.Alert.popLayer.popYCenter("服务出错,请稍候再试")
        }
    });
}

